
//--> Loth 2011 
//--> free simple Forum php

package chamans.webphp
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.text.*;
		
		public class Forum extends MovieClip
		{
			private var File_xml:String;
			private var File_php:String;
			private var LISTING:XML;
			// texte def
		//	private var police:String="FFF Extras";
			//private var police:String="Times";
			private var style:StyleSheet;
			// base 
			private var max:int=0;
			private var curent:int=0;
			private var oneMessage:Boolean=false;
			private var Newer:Boolean=false;
			private var signal:String;
			private var c01:int=0X656565;  private var C01:Array=[ c01, c01+0X202020, c01-0X202020 ];
			private var c02:int=0XD2802D;  private var C02:Array=[ c02, c02+0X202020, c02-0X202020 ];
			private var c03:int=0X226622;  private var C03:Array=[ c03, c03+0X202020, c03-0X202020 ];
			private var c04:int=0XDF6531;  private var C04:Array=[ c04, c04+0X202020, c04-0X202020 ];
		//	private var c05:int=0XDF6531;  private var C04:Array=[ c04, c04+0X202020, c04-0X202020 ];
			// texte
			private var Nom:txt;
			private var Mess:txt;
			private var titre:txt;
			private var sortie:txt;
			private var Message:txt;
			private var Calendar:txt;
			// movie
			public var content:Sprite
			private var CurrentBarre:MovieClip;
			private var newListe:MovieClip;
			private var DISPLAY:MovieClip;
			private var READER:MovieClip;
			private var barre0:MovieClip;
			private var barre1:MovieClip;
			private var barre2:MovieClip;
			private var ENTER:MovieClip;
			
			///////////////////////////////////////
			///          SERVER loth             //
			///////////////////////////////////////
			public function Forum(S:StyleSheet):void {
				
				File_xml="http://3dflashlo.free.fr/assets/server/server.xml"
				File_php="http://3dflashlo.free.fr/assets/server/server.php"
				
				content=new Sprite();
				addChild(content);
				content.x=-175;
				content.y=-200;
				style=S;
			/*	style = new StyleSheet();
				style.setStyle("button", {fontSize:'18px', fontFamily:police, leading:'-2px', color:'#DDDDDD', fontWeight : "bold", textAlign :"center"})
				style.setStyle("base2", {fontSize:'12px', fontFamily:police, leading:'0px', color:'#A3CEC4', textAlign :"center"})
				style.setStyle("titre", {fontSize:'14px', fontFamily:police, leading:'-2px', color:'#DDDDDD', fontWeight : "bold"})
				style.setStyle("base", {fontSize:'12px', fontFamily:police, leading:'0px', color:'#A3CEC4', textAlign :"right"})
				style.setStyle("about", {fontSize:'14px', fontFamily:police, leading:'0px', color:'#A3CEC4', textAlign :"right", fontWeight : "bold"})
			*/
				INIT();
			}
			private function INIT():void{
				
				var  xml00:XML=<xml><titre>FORUM</titre></xml>
				titre=new txt({ text:xml00, target:content, style:style, width:300,height:20,  x:25, y:0 })
				var  xml0:XML=<xml><about></about></xml>
				sortie=new txt({ text:xml0, target:content, style:style, width:300,height:20,  x:25, y:20 })
				//message bas
				Message=new txt({ text:"", target:content, style:style, width:250,height:96,  x:50, y:260 ,W:true ,M:true , S:true, type:1 })
			//	var  xml:XML=""
				Nom=new txt({ text:"", target:content, style:style, width:160,height:20,  x:48, y:235 , S:true, type:1 })
				Calendar=new txt({ text:"", target:content, style:style, width:70,height:20,  x:230, y:235  , S:true, type:1 })
			 //	var WEB:String;
				
				ChargeXML()
				newListe=new Liste(style);
				//newListe.Base=this
				//newListe.ROO=this
				
				newListe.TYPES=[ 'Id', 'Date', 'Titre', 'Message' ]
				newListe.x=45
			    newListe.y=35
				
				content.addChild(newListe)
				CREE_Display(DISPLAY)
			}
			
			private function Clear(target:MovieClip):void {
				while(target.numChildren) target.removeChildAt(0);
			}
			private function DEL(e:Event=null):void{
				// textes
				titre.Remove()
				sortie.Remove()
				Message.Remove()
				Nom.Remove()
				Calendar.Remove()
				newListe.CLEAR()
				Clear(CurrentBarre)
			}
			private function ADD_LISTE():void{
				newListe.xml=LISTING
				newListe.action=Get_N;
				newListe.DIRECT()
			}
			/// PANO DISPLAY
			private function CREE_Display (OBJ:MovieClip):void{
				var posY:int=370
				var B0:Button=new Button(style, "UPDATE", content, ChargeXML, 226, posY, C01,74)
			//	var B00:Button=new Button(style, "X", content, DEL, 200, 0, C03, 30)
				ChargeXML();
				barre0=new MovieClip();
				barre1=new MovieClip();
				barre2=new MovieClip();
				
				var B1:Button=new Button(style, "<<", barre0, Get_Prev, 50, 0, C01, 30)
				var B2:Button=new Button(style, ">>", barre0, Get_Next, 82, 0, C01, 30)
				
				var B3:Button=new Button(style, "<<", barre1, Get_Prev, 50, 0, C01, 30)
				var B4:Button=new Button(style, ">>", barre1, Get_Next, 82, 0, C01,  30)
				var B5:Button=new Button(style, "NEW POST", barre1, Make_post, 114, 0, C02, 110)
				
				var B6:Button=new Button(style, "SEND", barre2, EnvoieXML, 50, 0, C02, 70)
				var B7:Button=new Button(style, "RESET", barre2, resetFICHE, 122, 0, C02, 70)
				var B8:Button=new Button(style, "X", barre2, sortie_post, 194, 0, C04, 30)
				content.addChild(barre1);
				CurrentBarre=barre1;
				barre1.y=barre2.y=barre0.y=posY;
			}
			private function Swaap_barre (b1:MovieClip):void{
				content.removeChild(CurrentBarre);
				content.addChild(b1);
				CurrentBarre=b1;
			}
			private function Make_post (e:Event=null):void{
				resetFICHE(); Newer=true;
				var now:Date = new Date(); var Ye:String = now.getFullYear().toString(); 
				var M:int=now.getMonth()+1;  var K:String;
				if (M<=9) K="0"+M.toString();  else K=M.toString();
				var D:String=now.getDate().toString();
				var date:String=D+"."+K+"."+Ye.substring(2,4);
				Swaap_barre(barre2)
				Calendar.update(date)
				Nom.TX.mouseEnabled=Message.TX.mouseEnabled=true
			}
			private function sortie_post (e:Event=null):void{
				Newer=false;
				if(!oneMessage) Swaap_barre(barre1)
				else Swaap_barre(barre0)
				Nom.TX.mouseEnabled=Message.TX.mouseEnabled=false
				Get_List (max)
			}
			private function resetFICHE (e:Event=null):void{
				Nom.update("")
				Message.update("")
			}
			
			///////////////////////////////////////
			///          CHARGE XML              //
			///////////////////////////////////////
			private function ChargeXML (e:Event=null):void  {
				var loader:URLLoader= new URLLoader(  );
				configureListeners(loader);
				var rand:String="?uniq="+int(1000*Math.random())
				var request:URLRequest = new URLRequest( File_xml+rand );
				
				try { loader.load(request); } 
				catch (error:Error) {sortie.update( "File read error")}
			}
			private function configureListeners(dispatcher:IEventDispatcher):void {
				dispatcher.addEventListener(Event.COMPLETE, completeHandler);
				dispatcher.addEventListener(Event.OPEN, openHandler);
				dispatcher.addEventListener(ProgressEvent.PROGRESS, Progress);
				dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
				dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
				dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			}  
			private function handleResponse( event:Event ):void { sortie.update("INIT");}
			private function completeHandler(event:Event):void {sortie.update("oo");
				LISTING=new XML( event.target.data );
				Get_Max();
				//   LISTING.ignoreWhitespace=true
				// LISTING.ignoreComments =true
				
				ADD_LISTE()
			}
			private function openHandler(event:Event):void {sortie.update( "openHandler");}
			private function Progress(event:ProgressEvent):void  {sortie.update("On progresse"+event.bytesTotal)}
			private function securityErrorHandler(event:SecurityErrorEvent):void {sortie.update( "SecurityError : "+event)}
			private function httpStatusHandler(event:HTTPStatusEvent):void {sortie.update( "Status : "+event)}
			private function ioErrorHandler(event:IOErrorEvent):void {sortie.update(  "ioErrorHandler")}
			private function Get_Max() :void{
				max=0;
				for each (var sub:XML in LISTING.children()) {max++}
				Get_List(max);
				Nom.TX.mouseEnabled=false
				Message.TX.mouseEnabled=false
				Calendar.TX.mouseEnabled=false
				// TITRE.selectable=false;
				//MESSAGE.selectable=false;
			}
			
			private function Get_List  (i:Number) :void{
				for each (var sub:XML in LISTING.children()) {
					if(sub.@Id==i){ 
						if(!Newer){
							Nom.update(sub.@Titre)
							Message.update(sub.@Message)
							Calendar.update(sub.@Date)
							///trace((sub.@Message))
						}}}}
			
			private function Get_Next (e:MouseEvent):void{ curent++; if(curent>max)curent=1; Get_List(curent) }
			private function Get_Prev (e:MouseEvent):void{ curent--; if(curent<1)curent=max; Get_List(curent) }
			private function Get_N(n:int=0):void{ trace(max-n);Get_List  (max-n) }
			private function Get_First (e:MouseEvent):void{ Get_List  (1) }
			private function Get_End (e:MouseEvent):void{ Get_List  (max) }
			///////////////////////////////////////
			///          ENVOIE XML              //
			///////////////////////////////////////
			private function EnvoieXML (e:Event):void {
				//si rien dans le type
				if (Nom.TX.text == "") {
					sortie.update( "No Name !?")
					signal = "";
					if (Message.TX.text == "") {
						sortie.update( "No Message !?");
						signal = "";
					}
					//Si tout est OK
				} else {sortie.update( "Send");
					signal = "Send in progress";
					var variables:URLVariables = new URLVariables();
					variables.signal = signal;
					
					//+++
					variables.id = max+1 ; //ENTER.out.NUM.text;
					variables.date = Calendar.TX.text; 
					variables.titre = Nom.TX.text;
					variables.message = Message.TX.text;
					
					
					var loader:URLLoader = new URLLoader(  );
					configureListeners2(loader);
					
					var request:URLRequest = new URLRequest( File_php );
					request.data = variables;
					request.method = URLRequestMethod.POST;
					
					try {loader.load(request);} 
					catch (error:Error) {sortie.update( "File read error");}
					
				}
			}
			
			private function configureListeners2(dispatcher:IEventDispatcher):void 
			{
				dispatcher.addEventListener(Event.COMPLETE, completeHandler2);
				dispatcher.addEventListener(Event.OPEN, openHandler2);
				dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler2);
				dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler2);
				dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler2);
				dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler2);
			}
			
			
			private function handleResponse2( event:Event ):void { sortie.update( "OK");
				//var myVariables:URLVariables = new URLVariables ( loader.data );
			}
			private function completeHandler2(event:Event):void 
			{
				//   var loader:URLLoader = URLLoader(event.target);
				//var vars:URLVariables = new URLVariables(loader.data);
				oneMessage=true;
				sortie.update("oo");
				sortie_post();
				ChargeXML();//reload 
			}
			private function openHandler2(event:Event):void { sortie.update( "openHandler");}
			private function progressHandler2(event:ProgressEvent):void { sortie.update( "On progresse");}
			private function securityErrorHandler2(event:SecurityErrorEvent):void {sortie.update( "SecurityError : "+event);}
			private function httpStatusHandler2(event:HTTPStatusEvent):void {sortie.update( "Status : "+event);}
			private function ioErrorHandler2(event:IOErrorEvent):void { sortie.update( "ioErrorHandler");}
			
			///////////////////////////////////////////
			///////////////////////////////////////////
			
		}}